package com.scanner.ui;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.ImageView;

import com.scanner.hardware.R;
import com.scanner.util.LogUtil;

public class ScannerFloatView {
    private static Context mContext;
    private static WindowManager mWindowManager;
    private static WindowManager.LayoutParams wmParams;
    private static View mView;
    private static boolean isShow = false;//悬浮框是否已经显示

    private static OnClickListener mListener;//view的点击回调listener
    private static OnFloatClickListener mOnFloatClickListener;
    private ImageView ivLinght;
    private ImageView ivLinghtClose;

    public void setOnClickListener(OnClickListener listener){
        mListener = listener;
    }

    public interface OnClickListener{
        void onClick(View view);
    }

    public interface OnFloatClickListener{
        void onCancelClick(View view);
        void onLongClick();

    }

    public void setOnFloatClickListener(OnFloatClickListener onFloatClickListener){
        mOnFloatClickListener = onFloatClickListener;
    }

    private static ScannerFloatView floatView = null;

    private ScannerFloatView(){

    }

    public static synchronized ScannerFloatView getInstance(){
        if(floatView == null){
            floatView = new ScannerFloatView();
        }
        return floatView;
    }


    /**
     * 是否移动
     */
    private boolean isMove = true;


    /**
     * 显示悬浮框
     */
    public void showFloatView(Context context,int layoutId){
        mContext = context;

        if(isShow){
            return;
        }

        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmParams = new WindowManager.LayoutParams();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//哭了。。。。我靠8.0又改了
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }


        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.CENTER;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.x = context.getResources().getDisplayMetrics().widthPixels;
        wmParams.y = 0;

        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        mView = LayoutInflater.from(context).inflate(layoutId, null);

        ivLinght = mView.findViewById(R.id.iv_lll);
        ivLinghtClose = mView.findViewById(R.id.iv_ss);
        ivLinght.setVisibility(View.GONE);
        ivLinghtClose.setVisibility(View.VISIBLE);
        mWindowManager.addView(mView, wmParams);



        mView.setOnTouchListener(new View.OnTouchListener() {
            float downX = 0;
            float downY = 0;
            int oddOffsetX = 0;
            int oddOffsetY = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        downX =  event.getX();
                        downY =  event.getY();
                        oddOffsetX = wmParams.x;
                        oddOffsetY = wmParams.y;
                        LogUtil.w("点击按下悬浮框==downX"+downX+";downY="+downY+";oddOffsetX="+oddOffsetX+";oddOffsetY="+oddOffsetY);
                        handler.postDelayed(runnable, ViewConfiguration.getLongPressTimeout());
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(!isMove){
                            break;
                        }
                        float moveX = event.getX();
                        float moveY =  event.getY();
                        //不除以3，拖动的view抖动的有点厉害
                        wmParams.x += (moveX - downX)/3;
                        wmParams.y += (moveY - downY)/3;
                        LogUtil.w("点击“移动”悬浮框==moveX"+moveX+";moveY="+moveY+";wmParams.x="+wmParams.x+";wmParams.y="+wmParams.y);
                        LogUtil.d("移动的距离==x:"+Math.abs(moveX - downX)+";y=="+ Math.abs(moveY - downY));
                        int scaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
                        if (Math.abs(moveX - downX) > scaledTouchSlop || Math.abs(moveY - downY) > scaledTouchSlop) { //移动超过了阈值，表示移动了
                            isMove = true; //移除runnable
                            handler.removeCallbacks(runnable);
                            if(mView != null){
                                ivLinght.setVisibility(View.GONE);
                                ivLinghtClose.setVisibility(View.VISIBLE);
                                mWindowManager.updateViewLayout(mView,wmParams);
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        int newOffsetX = wmParams.x;
                        int newOffsetY = wmParams.y;

                        handler.removeCallbacks(runnable);
                        LogUtil.w("点击“抬起”悬浮框==newOffsetX="+newOffsetX+";newOffsetY="+newOffsetY+";oddOffsetX="+oddOffsetX+";oddOffsetY="+oddOffsetY);
                        LogUtil.d("移动的距离==x:"+Math.abs(newOffsetX - oddOffsetX)+";y=="+Math.abs(newOffsetY - oddOffsetY));
                        if(Math.abs(newOffsetX - oddOffsetX) <=20 || Math.abs(newOffsetY - oddOffsetY) <=20){
                            isMove = true;
                            if(mOnFloatClickListener != null){
                                if(mView != null){
                                    ivLinght.setVisibility(View.GONE);
                                    ivLinghtClose.setVisibility(View.VISIBLE);
                                    mWindowManager.updateViewLayout(mView,wmParams);
                                }
                                mOnFloatClickListener.onCancelClick(mView);
                            }
                        }
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        LogUtil.w("取消");
                        break;
                }
                return true;
            }

        });

        isShow = true;
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            LogUtil.i("长按了vvvvv");
            isMove = false;
            if(mOnFloatClickListener != null){
                ivLinght.setVisibility(View.VISIBLE);
                ivLinghtClose.setVisibility(View.GONE);
                mWindowManager.updateViewLayout(mView,wmParams);
                mOnFloatClickListener.onLongClick();
            }
        }
    };

    private Handler handler = new Handler();

    /**
     * 隐藏悬浮窗
     */
    public void hideFloatView(){
        if(mWindowManager != null && isShow){
            mWindowManager.removeView(mView);
            isShow = false;
        }
    }

}
